import baseservice from '../common/baseservice.js';
export default {
	getPageData(self, successBack, errorBack) {
		baseservice.getPageData(self, self.apis.getNewsInfoList, self.searchcondition, function(res) {
			self.tableData = self.formattool.trimNull(res.data.list, []);
			for(let i in self.tableData) {
				let content = self.tableData[i].articleContent.replace(/<[^>]+>|&nbsp;/g, "");
				if(content.length > 100) {
					self.tableData[i].articleContent = content.substring(0, 100) + "...";
				}
			}
			console.log(self.tableData);
			self.total = res.data.total;
		});

	},
	getModelData(self, id, successBack, errorBack) {
		let param = {
			id: self.id
		}
		self.httptool.ajaxPost(self.apis.getNewsInfoDetail, param, res => {
			self.modelData = self.formattool.trimNull(res.data, {});
			if(self.modelData.tags) {
				self.selectedtags = self.modelData.tags.split(',');
			}
		});
	},

	handleSave(self, successBack, errorBack) {debugger;
		let that = this;
		self.modelData.articleContent = self.$refs.ue.getUEContent().replace("<p><br></p>", "");
		if(self.selectedtags)	
			self.modelData.tags = self.selectedtags.join(',');
		let rule = [{
			name: 'articleTitle',
			label: '资讯标题',
			rule: ['validateEmpty_']
		}, {
			name: 'articleContent',
			label: '资讯内容',
			rule: ['validateEmpty_']
		}]
		console.log(self.modelData)
		if(!self.validatetool.validate(self, self.modelData, rule)) {
			return;
		}
		let msg = '添加成功';
		let requestUrl = 'information/insert'
		if(self.id) {
			msg = '修改成功';
			requestUrl = 'information/update';
		};
		self.httptool.ajaxPost(requestUrl, self.modelData, function(res) {
			if(res.code == 1) {
				self.$message({
					message: msg,
					type: 'success'
				});
				self.close();
				self.$emit('close', true);
			} else {
				self.$message(res.msg);
			}
		});

	},
	handleDelete(self, id) {
		let that = this;
		baseservice.handleDelete(self, self.apis.delNewsInfo, id, function(res) {
			that.getPageData(self);
		});
	},
}